package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.sysconfig.bean.MenuResource;
import cn.nubia.admin.sysconfig.dto.MenuResourceDTO;
import cn.nubia.admin.sysconfig.service.MenuResourceService;
import cn.nubia.admin.sysconfig.bean.UserResource;
import cn.nubia.admin.sysconfig.service.UserResourceService;
import cn.nubia.admin.sysconfig.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作界面控制层.
 */
@Controller
@RequestMapping("/user")
@RequiresPermissions("sys:syscfg:menures:menuresList")
public class UserResourceController {


    @Autowired
    private UserService userService;

    @Autowired
    UserResourceService userResourceService;

    @Autowired
    MenuResourceService menuResourceService;


    @ResponseBody
    @RequestMapping(value = "getResources", method = RequestMethod.GET)
//    @RequiresPermissions("user:resource")
    public Response<List<MenuResourceDTO>> getResources(Integer userId) {
        List<MenuResource> menuResources = menuResourceService.getMenuResourceList();

        UserResource userResource = userResourceService.get(userId);

        String[] resources = new String[0];
        if (null != userResource && null != userResource.getResourceIds()) {
            resources = userResource.getResourceIds().split(",");
        }

        List<MenuResourceDTO> retList = new ArrayList<MenuResourceDTO>();


        for (MenuResource resource : menuResources) {
            MenuResourceDTO menuResourceDTO = new MenuResourceDTO();
            BeanUtils.copyProperties(resource, menuResourceDTO);
            menuResourceDTO.setUserId(userId);
            for (int i = 0; i < resources.length; i++) {
                if (resources[i].equalsIgnoreCase(resource.getId().toString())) {
                    menuResourceDTO.setChecked(true);
                    break;
                }
            }
            retList.add(menuResourceDTO);
        }
        return Response.getSuccessResponse(retList);
    }


    /**
     * 添加用户权限.
     *
     * @param userResource 角色权限
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addUserResource", method = RequestMethod.POST)
//    @RequiresPermissions("user:resource")
    public Response<Boolean> addUserResource(UserResource userResource) {
        userResourceService.saveUserResource(userResource);
        return Response.getSuccessResponse(true);
    }


    /**
     * 删除用户权限
     *
     * @param userResource 用户权限
     * @return
     */
    @ResponseBody
//    @RequiresPermissions("user:resource")
    @RequestMapping(value = "deleteUserResource", method = RequestMethod.POST)
    public Response<Boolean> deleteUserResource(UserResource userResource) {
        userResourceService.deleteUserResource(userResource);
        return Response.getSuccessResponse(true);
    }


}
